package com.fengziyu.lib_countdown_fzy

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.fengziyu.lib_countdown_fzy.databinding.ActivityCountdownBinding
import java.util.Timer
import java.util.TimerTask

@Route(path = "/countdown/Countdown_Activity")
class CountDownActivity : AppCompatActivity() {
    private lateinit var binding: ActivityCountdownBinding
    private lateinit var mHandler: Handler
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCountdownBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.countdownTimerBtn.setOnClickListener {
            countdownTimerTest(10)
        }
        binding.handlerBtn.setOnClickListener {
            handlerTimerTest()
            startCountDown()
        }
        binding.timerBtn.setOnClickListener {
            timerTaskTest(10)
        }
    }

    /**
     * Timer倒计时
     */
    private fun timerTaskTest(number: Int) {
        var num = number
        val timer = Timer()
        val timerTask = object : TimerTask() {
            override fun run() {
                num--
                binding.timerBtn.text = "倒计时: $num"
                println("倒计时: $num")
                if(num<0){
                    binding.timerBtn.text = "倒计时结束"
                    timer.cancel()
                }
            }
        }
        timer.schedule(timerTask , 1000 , 1000)
    }

    /**
     * Handler倒计时
     */
    private fun handlerTimerTest() {
        var handlerNum = 10
        mHandler = object : Handler(Looper.getMainLooper()){
            override fun handleMessage(msg: Message) {
                if(handlerNum>0){
                    handlerNum--
                    binding.handlerBtn.text = "倒计时: $handlerNum"
                    mHandler.sendEmptyMessageDelayed(1,1000)
                    println("倒计时: $handlerNum")
                }else{
                    binding.handlerBtn.text = "倒计时结束"
                }
            }
        }
    }
    fun startCountDown(){
        mHandler.sendEmptyMessage(1)
    }

    /**
     * CountdownTimer倒计时
     */
    private fun countdownTimerTest(number: Int){
        var num = number
        val timer = object : CountDownTimer(num * 1000L , 1000L){
            @SuppressLint("SetTextI18n")
            override fun onTick(p0: Long) {
                num--
                //TODO 更新UI
                binding.countdownTimerBtn.text = "倒计时: $num"
                println("time: $num")
                if(num == 0){
                    onFinish()
                }
            }

            override fun onFinish() {
                binding.countdownTimerBtn.text = "倒计时结束"
            }
        }
        timer.start()
    }


    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }
}